package com.dgl.sdk.view.autoFitWidthImageView;

import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class SimpleZoomListener implements View.OnTouchListener {

    public enum ControlType {
        PAN, ZOOM
    }

    private ControlType mControlType = ControlType.PAN;

    private ZoomState mState;

    private float mX;
    private float mY;
    private float mGap;
    
    private int scr_width;
    private int scr_height;
    private DisplayMetrics metric;

//    public SimpleZoomListener(){
//         metric = new DisplayMetrics();
//         this.getWindowManager().getDefaultDisplay().getMetrics(metric);
//         scr_width = metric.widthPixels;     // 屏幕宽度（像素）
//         scr_width = metric.heightPixels;   // 屏幕高度（像素）
//    }
   
    public void setZoomState(ZoomState state) {
        mState = state;
    }

    public void setControlType(ControlType controlType) {
        mControlType = controlType;
    }

    public boolean onTouch(View v, MotionEvent event) {   
    	
    	ImageZoomView imageView = (ImageZoomView)v;
    	
        final int action = event.getAction();
        int pointCount = event.getPointerCount();
        if(pointCount == 1){
        	final float x = event.getX();
            final float y = event.getY();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    mX = x;
                    mY = y;
                    break;
                case MotionEvent.ACTION_MOVE: {
                    final float dx = (x - mX) / v.getWidth();
                    float dy = (y - mY) / v.getHeight();
                    	dy = dy/2;
                        mState.setPanX(mState.getPanX() - dx);
                        mState.setPanY(mState.getPanY() - dy);
                        mState.notifyObservers();
                    	mX = x;                 
                        mY = y;

                    break;
                }
            }
        }
		if (pointCount == 2) {

			try {
				final float x0 = event.getX(event.getPointerId(0));
				final float y0 = event.getY(event.getPointerId(0));

				final float x1 = event.getX(event.getPointerId(1));
				final float y1 = event.getY(event.getPointerId(1));

				final float gap = getGap(x0, x1, y0, y1);
				switch (action) {
				case MotionEvent.ACTION_POINTER_2_DOWN:
				case MotionEvent.ACTION_POINTER_1_DOWN:
					mGap = gap;
					break;
				case MotionEvent.ACTION_POINTER_1_UP:
					mX = x1;
					mY = y1;
					break;
				case MotionEvent.ACTION_POINTER_2_UP:
					mX = x0;
					mY = y0;
					break;
				case MotionEvent.ACTION_MOVE: {
					final float dgap = (gap - mGap) / mGap;
					// Log.d("Gap", String.valueOf(dgap));
					Log.d("Gap", String.valueOf((float) Math.pow(20, dgap)));
					mState.setZoom(mState.getZoom() * (float) Math.pow(5, dgap));
					mState.notifyObservers();
					mGap = gap;
					break;
				}
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			}

		}
        	
        return true;
    }
    
    private float getGap(float x0, float x1, float y0, float y1){
    	return (float)Math.pow(Math.pow((x0-x1), 2)+Math.pow((y0-y1), 2), 0.5);
    }

}
